package com.pcitc.example.config;

import javax.sql.DataSource;
import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 
 * 项目名称： security-service
 * 包名称：   com.sinopec.smcc.notice.controller
 * 类名称：   NoticeController
 * 类描述：   安全通报控制类
 * 创建人：   lixiang
 * 创建时间： 2018年3月23日 下午6:07:53
 * 修改人： 
 * 修改时间:
 * 修改备注:
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig {

	@Resource(name="myRoutingDataSource")
	private DataSource myRoutingDataSource;
	
	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception{
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
		return sqlSessionFactoryBean.getObject();
	}
	
	@Bean
	public PlatformTransactionManager platformTransactionManager(){
		return new DataSourceTransactionManager(myRoutingDataSource);
	}
}
